package leetcode.editor.week.week303;

import java.util.*;

public class Solution6126 {

    // 超时，差2股份案例
    /*class FoodRatings {
        Map<String, LinkedList<String>> cuisineToFood;
        Map<String, Integer> foodToRating;

        public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
            cuisineToFood = new HashMap<>();
            foodToRating = new HashMap<>();
            int i = 0;
            while (i < foods.length) {
                if (cuisineToFood.get(cuisines[i]) == null) {
                    cuisineToFood.put(cuisines[i], new LinkedList<>());
                }

                cuisineToFood.get(cuisines[i]).add(foods[i]);
                foodToRating.put(foods[i], ratings[i]);

                i++;
            }
        }

        public void changeRating(String food, int newRating) {
            if (foodToRating.containsKey(food)) {
                foodToRating.put(food, newRating);
            }
        }

        public String highestRated(String cuisine) {
            LinkedList<String> list = cuisineToFood.get(cuisine);
            int maxRating = 0;
            String foodName = "";
            for (String s : list) {
                if (foodToRating.containsKey(s)) {
                    Integer val = foodToRating.get(s);
                    if (val > maxRating) {
                        maxRating = val;
                        foodName = s;
                    } else if (val == maxRating) {
                        foodName = foodName.compareTo(s) > 0 ? s : foodName;
                    }
                }
            }
            return foodName;
        }*/

    // 参照：https://leetcode.cn/problems/design-a-food-rating-system/solution/by-moonstruck-v48b/
    class FoodRatings {
        // 食品类
        class Food {
            String food;
            String cui;
            int rating;

            public Food(String food, String cui, int rating) {
                this.food = food;
                this.cui = cui;
                this.rating = rating;
            }

            public Food() {
            }
        }

        // 食品 食品类 （主要是为了通过食品查找品类）
        Map<String, Food> map1 = new HashMap<>();
        // 烹饪 食品类
        Map<String, TreeSet<Food>> map2 = new HashMap<>();

        public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
            for (int i = 0; i < foods.length; i++) {
                Food food = new Food(foods[i], cuisines[i], ratings[i]);
                map1.put(foods[i], food);
                // 使用TreeSet
                // 如果rating不相同，按照rating降序，如果rating相同，按照字典序升序
                map2.putIfAbsent(cuisines[i], new TreeSet<Food>((a, b) -> a.rating == b.rating ? a.food.compareTo(b.food) : b.rating - a.rating));
                map2.get(cuisines[i]).add(food);
            }
        }

        public void changeRating(String food, int newRating) {
            // 获取旧food
            Food oldFood = map1.get(food);
            // 删除烹饪中的旧food类
            map2.get(oldFood.cui).remove(oldFood);
            // 在添加到烹饪map中，先删除在添加就是为了进行再次排序
            oldFood.rating = newRating;
            map2.get(oldFood.cui).add(oldFood);
            // 修改食物的map
            map1.put(food, oldFood);
        }

        public String highestRated(String cuisine) {
            return map2.get(cuisine).first().food;
        }
    }

/**
 * Your FoodRatings object will be instantiated and called as such:
 * FoodRatings obj = new FoodRatings(foods, cuisines, ratings);
 * obj.changeRating(food,newRating);
 * String param_2 = obj.highestRated(cuisine);
 */
}